﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR_ASP.ViewModel;
using DB.Model;

namespace HR_ASP.Controllers
{
    public class ObecnoscController : Controller
    {
        //
        // GET: /Obecnosc/

        public ActionResult Index()
        {
            using (var context = new HREntities())
            {
                return View(context.Obecnosci.Select(n => new ObecnoscViewModel()
                {
                    ID = n.ID_Obec,
                    IDPracow =(int) n.ID_Pracow,
                    DataRoz = (DateTime)n.Data_Rozp,
                    DataZak = (DateTime) n.DataK_Zakon,
                    DataM = (DateTime)n.DataM                
                }).ToArray()
                );
            }
        }

        //
        // GET: /Obecnosc/Details/5

        public ActionResult Details(int id)
        {
            using (var context = new HREntities())
            {
                Obecnosc obecnoscDB = context.Obecnosci.SingleOrDefault(n => n.ID_Obec == id);
                if (obecnoscDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new ObecnoscViewModel(obecnoscDB));
            }
        }

        //
        // GET: /Obecnosc/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Obecnosc/Create

        [HttpPost]
        public ActionResult Create(ObecnoscViewModel obecnosc)
        {
            try
            {
                using (var context = new HREntities())
                {
                    context.Obecnosci.AddObject(obecnosc.getModel(new Obecnosc()));
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Obecnosc/Edit/5
 
        public ActionResult Edit(int id)
        {
            using (var context = new HREntities())
            {
                Obecnosc obecnoscDB = context.Obecnosci.SingleOrDefault(n => n.ID_Obec == id);
                if (obecnoscDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new ObecnoscViewModel(obecnoscDB));
            }
        }

        //
        // POST: /Obecnosc/Edit/5

        [HttpPost]
        public ActionResult Edit(ObecnoscViewModel obecnosc)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Obecnosc obecnoscDB = context.Obecnosci.SingleOrDefault(n => n.ID_Obec == obecnosc.ID);
                    context.ApplyCurrentValues<Obecnosc>("Obecnosci", obecnosc.getModel(obecnoscDB));
                    context.SaveChanges();
                }
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Obecnosc/Delete/5
 
        public ActionResult Delete(int id)
        {
            using (var context = new HREntities())
            {
                Obecnosc obecnoscDB = context.Obecnosci.SingleOrDefault(n => n.ID_Obec == id);
                if (obecnoscDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new ObecnoscViewModel(obecnoscDB));
            }
        }

        //
        // POST: /Obecnosc/Delete/5

        [HttpPost]
        public ActionResult Delete(ObecnoscViewModel obecnosc)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Obecnosc obecnoscDB = context.Obecnosci.SingleOrDefault(n => n.ID_Obec == obecnosc.ID);
                    if (obecnoscDB != null)
                    {
                        context.Obecnosci.DeleteObject(obecnoscDB);
                        context.SaveChanges();
                    }
                }
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
